package com.ptswitch.material.model.security;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.tapestry5.beaneditor.DataType;
import org.hxzon.tapestry5.util.TapestryEx;

@Entity
@Table(name = "role_permission")
public class RolePermission implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManyToOne(fetch = FetchType.LAZY)
    @Id
    private Role role;

    @ManyToOne(fetch = FetchType.EAGER)
    @Id
    private Permission permission;

    private String test;

    @DataType(TapestryEx.DataType_daytime)
    private Date createTime;

    //=============
    public RolePermission() {
        setCreateTime(new Date());
    }

    public RolePermission(Role role, Permission permission) {
        this.role = role;
        this.permission = permission;
        setCreateTime(new Date());
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(role.getId())//
                .append(permission.getId())//
                .toHashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof RolePermission)) {
            return false;
        }
        RolePermission other = (RolePermission) o;
        return new EqualsBuilder().append(this.role.getId(), other.role.getId())//
                .append(this.permission.getId(), other.permission.getId())//
                .isEquals();
    }

    //================
    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

}
